<?php

namespace Biz\model;

use Biz\validate\ShopCompanyInfo as vShopCompanyInfo;
use Medoo\Medoo;

/**
 *
 * @author lee
 *
 */
class ShopCompanyInfo extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_company_info';

    const _SUB_TABLE = false;


    public static function createOrEdit($uid, $data, $scenes = null, $method = null)
    {

        try {
            $ret = null;

            if (is_null($method)) {
                $has = self::has_master(Medoo::raw(' where uid=:uid for update', [':uid' => $uid]));


                $method = $has ? 'update' : 'insert';
            }

            if (!is_null($scenes)) {
                //提交字段进行校验
                if (!vShopCompanyInfo::validate($data, $scenes)) {

                    $msg = vShopCompanyInfo::firstError();

                    throw new \Exception($msg);
                }
            }


            switch ($method) {
                case 'insert':
                    $data['shop_id'] = $uid;
                    $data['uid'] = $uid;
                    $data['addtime'] = date('Y-m-d H:i:s');
                    $data['telephone'] = (isset($data['telephone']) && !is_null($data['telephone'])) ? $data['telephone'] : '';

                    $ret = self::insert($data);

                    break;
                case 'update':
                    $ret = self::update($data, ['uid' => $uid]);

                    break;
            }

            if ($ret->errorCode() !== '00000') {
                throw new \Exception('数据库操作错误');
            }

            return [$method => $ret->rowCount()];

        } catch (\Exception $e) {


            throw $e;
        }
    }


    public static function getInfo($uid)
    {
        $info = self::get('*', ['uid' => $uid]);

        return $info;
    }

    public static function getInfoList($uids)
    {
        $list = self::select("*", ['uid' => $uids]);
        $formatData = [];
        foreach ($list as $info) {
            $formatData[$info["uid"]] = $info;
        }
        return $formatData;
    }

}

